package com.example.administrator.yuejian23.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.yuejian23.Bean.LoginBean;
import com.example.administrator.yuejian23.Const;
import com.example.administrator.yuejian23.MainActivity;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/11/8.
 */

public class LoginActivity extends AppCompatActivity{
    private Button btn_reg,btn_login;
    private EditText et_username,et_password;
    private String error;

    private EditText telView;
    private EditText passwordView;
    private Button loginButton;
    private Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_login);
        telView = (EditText)findViewById(R.id.editText_tel);
        passwordView = (EditText)findViewById(R.id.editText_password);
        loginButton = (Button) findViewById(R.id.button_login);
        registerButton = (Button)findViewById(R.id.button_register);
        initEvents();
    }


    public void initEvents() {
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit =
                        new Retrofit.Builder()
                                .baseUrl(Const.BASE_URL)
                                .addConverterFactory(GsonConverterFactory.create())
                                .build();
                RetrofitService retrofitService =
                        retrofit.create(RetrofitService.class);
                final String username = telView.getText().toString();
                final String password =passwordView.getText().toString();
                Call<LoginBean> loginCall = retrofitService.userLogin(username,password);

                loginCall.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        error = response.body().getError();
                        if("用户名或密码不正确".equals(error)){
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            if(username.equals("") || password.equals("")){
                                Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                String userid = response.body().getId().trim();
                                Log.v("获取id",userid);
                                saveUser(userid);
                           /*     setUser(userid);*/
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);                                //传用户Id
                                startActivity(intent);
                                finish();

                            }

                        }

                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {

                    }
                });

            }
        });
    }
public void setUser(String userId){}
    public void saveUser(String user_id){

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("userID",user_id);
        editor.putString("username",telView.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id",userid);
        editor.putString("userpass",passwordView.getText().toString());
        editor.commit();
    }




}

